package com.nursinghome.user.controller;


import com.nursinghome.admin.service.AccountService;
import com.nursinghome.bean.Account;
import com.nursinghome.bean.Order;
import com.nursinghome.bean.Reservation;
import com.nursinghome.bean.RespBean;
import com.nursinghome.common.service.OrderService;
import com.nursinghome.common.service.ReservationService;
import com.nursinghome.common.service.RoomOccupancyService;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.user.service.UserAccountService;
import com.nursinghome.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user/account")
public class UserAccountController {
    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private RoomOccupancyService  roomOccupancyService;

    @GetMapping
    public RespBean selectByadata(@RequestHeader(value = "token", required = false) String token,
                                  String data) {
        //时间转换
        if (data.length() > 0 && data != null) {
            ZonedDateTime zdt = ZonedDateTime.parse(data);
            // 加上八小时
            ZonedDateTime newZdt = zdt.plusHours(8);
            // 转换为字符串
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
            data = newZdt.format(formatter);
        }
        //判断令牌
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer eidInt = (Integer) map.get("eid");
        Long  eid = new Long(eidInt + "");
        if (eid == null) {
            return RespBean.error("令牌信息不正确");
        }
        List<Account> account= userAccountService.selectByadata(data,eid);
        return RespBean.ok("",account);
    }
    @PutMapping("/pay")
    public RespBean pay(@RequestHeader String token) throws NurisingHomeException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Long eid = new Long(String.valueOf(map.get("eid")));
        // 查询未支付的订单和服务账单
        Order order = new Order();
        order.setEid(eid);
        order.setStatus(3);
        List<Order> orders = orderService.selectByCondition(order);
        for (Order order1 : orders) {
            order1.setStatus(5);
            orderService.update(order1);
        }
        // 查询未支付的预约服务
        Reservation reservation = new Reservation();
        reservation.setEid(eid);
        reservation.setStatus(2);
        List<Reservation> reservations = reservationService.selectAllByCondition(reservation);
        for (Reservation reservation1 : reservations) {
            reservation1.setStatus(3);
            reservationService.update(reservation1);
        }
        return RespBean.ok("支付成功");
    }
    @GetMapping("/byaid")
    public RespBean selectByAidByUser(Long aid){
        Account account = accountService.selectByAid(aid);

        return RespBean.ok("",account);
    }
}
